home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / system-config-printer / gtkinklevel.py < prev    next >
Text File  |  2009-10-19  |  4KB  |  126 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Copyright (C) 2009 Tim Waugh <twaugh@redhat.com>
  4. ## Copyright (C) 2009 Red Hat, Inc.
  5.  
  6. ## This program is free software; you can redistribute it and/or modify
  7. ## it under the terms of the GNU General Public License as published by
  8. ## the Free Software Foundation; either version 2 of the License, or
  9. ## (at your option) any later version.
  10.  
  11. ## This program is distributed in the hope that it will be useful,
  12. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ## GNU General Public License for more details.
  15.  
  16. ## You should have received a copy of the GNU General Public License
  17. ## along with this program; if not, write to the Free Software
  18. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. import gtk
  21. import cairo
  22.  
  23. class GtkInkLevel (gtk.DrawingArea):
  24.     def __init__ (self, color, level=0):
  25.         gtk.DrawingArea.__init__ (self)
  26.         self.connect ('expose-event', self.expose_event)
  27.         self._level = level
  28.         try:
  29.             self._color = gtk.gdk.color_parse (color)
  30.         except (ValueError, TypeError):
  31.             self._color = gtk.gdk.color_parse ('#cccccc')
  32.  
  33.         self.set_size_request (30, 45)
  34.  
  35.     def set_level (self, level):
  36.         self._level = level
  37.         self.queue_resize ()
  38.  
  39.     def get_level (self):
  40.         return self._level
  41.  
  42.     def expose_event (self, widget, event):
  43.         ctx = self.window.cairo_create ()
  44.         ctx.rectangle (event.area.x, event.area.y,
  45.                        event.area.width,
  46.                        event.area.height)
  47.         ctx.clip ()
  48.  
  49.         (w, h) = self.window.get_size ()
  50.         ratio = 1.0 * h / w
  51.         if ratio < 1.5:
  52.             w = h * 2.0 / 3.0
  53.         else:
  54.             h = w * 3.0 / 2.0
  55.         thickness = 1
  56.         ctx.translate (thickness, thickness)
  57.         ctx.scale (w - 2 * thickness, h - 2 * thickness)
  58.         thickness = max (ctx.device_to_user_distance (thickness, thickness))
  59.         ctx.set_line_width (thickness)
  60.         self.draw (ctx)
  61.  
  62.     def draw (self, ctx):
  63.         r = self._color.red / 65535.0
  64.         g = self._color.green / 65535.0
  65.         b = self._color.blue / 65535.0
  66.         fill_point = self._level / 100.0
  67.  
  68.         ctx.move_to (0.5, 0.0)
  69.         ctx.curve_to (0.5, 0.33, 1.0, 0.5, 1.0, 0.67)
  70.         ctx.curve_to (1.0, 0.85, 0.85, 1.0, 0.5, 1.0)
  71.         ctx.curve_to (0.15, 1.0, 0.0, 0.85, 0.0, 0.67)
  72.         ctx.curve_to (0.0, 0.5, 0.1, 0.2, 0.5, 0.0)
  73.         ctx.close_path ()
  74.         ctx.set_source_rgb (r, g, b)
  75.         ctx.stroke_preserve ()
  76.         if fill_point > 0.0:
  77.             grad_width = 0.10
  78.             grad_start = fill_point - (grad_width / 2)
  79.             if grad_start < 0:
  80.                 grad_start = 0
  81.  
  82.             pat = cairo.LinearGradient (0, 1, 0, 0)
  83.             pat.add_color_stop_rgba (0, r, g, b, 1)
  84.             pat.add_color_stop_rgba ((self._level - 5) / 100.0, r, g, b, 1)
  85.             pat.add_color_stop_rgba ((self._level + 5)/ 100.0, 1, 1, 1, 1)
  86.             pat.add_color_stop_rgba (1.0, 1, 1, 1, 1)
  87.             ctx.set_source (pat)
  88.             ctx.fill ()
  89.         else:
  90.             ctx.set_source_rgb (1, 1, 1)
  91.             ctx.fill ()
  92.  
  93. if __name__ == '__main__':
  94.     # Try it out.
  95.     import gobject
  96.     import time
  97.     def adjust_level (level):
  98.         l = level.get_level ()
  99.         l += 1
  100.         if l > 100:
  101.             l = 0
  102.         level.set_level (l)
  103.         return True
  104.  
  105.     w = gtk.Window ()
  106.     w.set_border_width (12)
  107.     vbox = gtk.VBox (spacing=6)
  108.     w.add (vbox)
  109.     hbox = gtk.HBox (spacing=6)
  110.     vbox.pack_start (hbox, False, False, 0)
  111.     klevel = GtkInkLevel ("black", level=100)
  112.     clevel = GtkInkLevel ("cyan", level=60)
  113.     mlevel = GtkInkLevel ("magenta", level=30)
  114.     ylevel = GtkInkLevel ("yellow", level=100)
  115.     hbox.pack_start (klevel)
  116.     hbox.pack_start (clevel)
  117.     hbox.pack_start (mlevel)
  118.     hbox.pack_start (ylevel)
  119.     gobject.timeout_add (10, adjust_level, klevel)
  120.     gobject.timeout_add (10, adjust_level, clevel)
  121.     gobject.timeout_add (10, adjust_level, mlevel)
  122.     gobject.timeout_add (10, adjust_level, ylevel)
  123.     w.show_all ()
  124.     w.connect ('delete_event', gtk.main_quit)
  125.     gtk.main ()
  126.